﻿function getParams() {

    var params = $("#formSearch").serializeObject();
       
    return params;
}

function replace_field(key) {

    var data = {
        "订单号": "Order#",
        "公司名称": "Company Name",
        "客户单号": "Customer Order Number",
        "付款银行": "Payment Bank",
        "付款姓名": "Payment Name",
        "付款卡号": "Payment Card Number",
        "付款金额": "Payment Amount",
        "手续费": "Trans Fee",
        "创建时间": "Creation Time",
        "付款结果": "Payment Result",
        "客户姓名": "Customer Name",
        "客户银行": "Customer Bank",
        "客户卡号": "Customer Card Number",
        "付款时间": "Payment Time",
        "付款等级": "Payment Level",
        "通知状态": "Notification Status",
        "通知时间": "Notification Time",
        "通知次数": "Notification Number",
        "付款类型": "Payment type",

        "未付款": "Unpaid",
        "付款成功": "Success",
        "付款失败": "Fail",
        "付款中": "Paying",
        "取消付款": "Cancel Payment",
        "推送异常": "Error"
    };

    var language = FlashPay.Util.Language();

    if (!FlashPay.Util.isNullOrEmptySpance(language) && language == "en") {
        return data[key];
    } else {
        return key;
    }
}

function doSearch() {

    var frozenColumns = [[
        { field: 'orderNo', title: replace_field('订单号'), width: 130, align: 'left' },
        {
            field: 'companyName',
            title: replace_field('公司名称'),
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                var result = "<span class='bold'>{0}</span>".format(val == null ? "" : val);
                if (row.paymentStatus == 3 && row.noticeTimes == 5 && row.changePaymentRecordCompanyPermission == "PaymentRecord0015") {
                    result += ' <a class="btn btn-warning btn-xs" data-modal="/view/payment/paymentrecordchangecompany.html?orderNo={0}&OriginalCompanyId={1}" data-title="{0}-改变公司" data-confirmname="确认" data-width="300" data-height="100">改变公司</a>'.format(row.orderNo, row.companyId);
                }
                return result;
            }

        },
        { field: 'withdrawalOrderNo', title: replace_field('客户单号'), width: 180, align: 'left' },
        { field: 'bankName', title: replace_field('付款银行'), width: 65, align: 'left' },
        { field: 'cardName', title: replace_field('付款姓名'), width: 65, align: 'left' },
        { field: 'cardNumber', title: replace_field('付款卡号'), width: 120, align: 'left' },
        {
            field: 'withdrawalAmount',
            title: replace_field('付款金额'),
            width: 65,
            align: 'right',
            formatter: function (val, row) {
                return '<span class="bold">{0}</span>'.format(val);
            }
        }
    ]];
    
    var columns = [[
        {
            field: 'withdrawalFee',
            title: replace_field('手续费'),
            width: 65,
            align: 'right',
            formatter: function (val, row) {
                return '<span class="bold">{0}</span>'.format(val);
            }
        },
        {
            field: 'createDbdate',
            title: replace_field('创建时间'),
            width: 120,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.FormatDate(val);
            }
        },
        {
            field: 'paymentStatus',
            title: replace_field('付款结果'),
            width: 210,
            align: 'left',
            formatter: function (val, item) {
                /**
                 * 1、未付款
                 * 2、付款成功
                 * 3、付款失败
                 * 4、付款中
                 * 5、取消付款
                 * 6、推送异常
                 * */
                var result = '';
                if (val == 1) {
                    result = '<span class="bold">{0}</span> <a class="btn btn-danger btn-xs" data-confirm="{0}<br>确定要<span class=red>取消</span>该付款单？" data-href="/Payment/PaymentRecordCancel?orderNo={1}">取消</a>'.format(replace_field('未付款'),item.orderNo);
                    if (item.confirmStatus == 1) {
                        result += ' <a class="btn btn-primary btn-xs" data-modal="/view/payment/paymentconfirm.html?orderNo={0}" data-title="确认付款" data-confirmname="确认付款" data-width="400" data-height="175"> 付款审核</a>'.format(item.orderNo);
                    }
                    if ((item.confirmStatus == 2 || item.confirmStatus == 3) && item.paymentType == 0) {
                        if (item.paymentCardId == null || (item.originalBankCardEnableStatus == 2 && item.renewMatchCardTimeDifference > 30)) {
                            result += ' <a class="btn btn-default btn-xs" data-modal="/view/payment/paymentrecordrenewmatchcard.html?paymentRecordId={0}&originalPaymentCardId={1}" data-title="{0}-重新配卡" data-confirmname="确认" data-width="950" data-height="700">重新配卡</a>'.format(item.orderNo, item.bankId);
                        }
                    }

                    if (item.changeChannelsPermission == 'PaymentRecord0014' && item.paymentCardId == null && (item.confirmStatus == 2 || item.confirmStatus == 3) && item.timeDifference > 10) {
                        result += ' <a class="btn btn-warning btn-xs" data-modal="/view/payment/paymentrecordchangechannels.html?orderNo={0}&paymentType={1}" data-title="{0}-改变渠道" data-confirmname="确认" data-width="300" data-height="100">改变渠道</a>'.format(item.orderNo, item.paymentType);
                    }
                } else if (val == 2) {
                    result = '<span class="green bold" title="{0}{1}">{0}{1}</span>'.format(replace_field('付款成功'), (item.paymentFailInfo != null && item.paymentFailInfo != "") ? " - " + item.paymentFailInfo : "");

                    var paymentTypes = [3, 5];

                    if (item.paymentAndReadTimeDifference > 5 && item.timeDifference <= 7200 && paymentTypes.indexOf(item.paymentType)==-1) {
                        result += ' <a class="btn btn-default btn-xs" onclick="return view(this)" data-href="/view/payment/paymentrecordlog.html?orderNo={0}" data-title="{0}-配卡明细" data-width="800" data-height="430">配卡明细</a>'.format(item.orderNo);
                    }
                } else if (val == 3) {
                    result = '<span class="red bold" title="{0}{1}">{0}{1}</span>'.format(replace_field('付款失败'),(item.paymentFailInfo != null && item.paymentFailInfo != "") ? " - " + item.paymentFailInfo : "");
                } else if (val == 4) {
                    result = '<span class="bold">{0}</span>'.format(replace_field('付款中'));
                    if (item.paymentType == 0) {
                        result += ' <a class="btn btn-warning btn-xs" onclick="return view(this)" data-href="/view/payment/paymentrecordreals.html?orderNo={0}" data-title="{0}-查询" data-width="900" data-height="300">查询</a>'.format(item.orderNo);
                        if (item.paymentAndReadTimeDifference > 5 && item.timeDifference <= 7200) {
                            result += ' <a class="btn btn-default btn-xs" onclick="return view(this)" data-href="/view/payment/paymentrecordlog.html?orderNo={0}" data-title="{0}-配卡明细" data-width="800" data-height="430">配卡明细</a>'.format(item.orderNo);
                        }
                    }
                    
                    result += ' <span class="red bold" title="{0}">{0}</span>'.format((item.paymentFailInfo != null && item.paymentFailInfo != "") ? "  " + item.paymentFailInfo : "");
                } else if (val == 5) {
                    result = '<span class="bold">{0}</span>'.format(replace_field('取消付款'));
                } else if (val == 6) {
                    result = '<span class="bold">{0}</span>'.format(replace_field('推送异常'));
                }

                return result;
            }
        },
        { field: 'withdrawalAccountName', title: replace_field('客户姓名'), width: 65, align: 'left' },
        { field: 'withdrawalBankName', title: replace_field('客户银行'), width: 65, align: 'left' },
        { field: 'withdrawalCardNumber', title: replace_field('客户卡号'), width: 150, align: 'left' },
        {
            field: 'paymentDate',
            title: replace_field('付款时间'),
            width: 120,
            align: 'left',
            formatter: function (val, item) {
                if (item.paymentStatus == 4) {
                    return FlashPay.Util.FormatDate(item.readDate);
                } else {
                    return FlashPay.Util.FormatDate(val);
                }
            }
        },
        { field: 'depositType', title: replace_field('付款等级'), width: 65, align: 'left' },
        {
            field: 'noticeStatus',
            title: replace_field('通知状态'),
            width: 125,
            align: 'left',
            formatter: function (val, item) {
                /**
                 * 1、未通知
                 * 2、通知成功
                 * 3、通知失败
                 * 4、通知中
                 * */
                var result = "";
                if (val == 1) {
                    result = '<span class="bold">未通知</span>';
                } else if (val == 2) {
                    result = '<span class="green bold">通知成功</span>';
                } else if (val == 3) {
                    result = "<span class='red bold' title='{0}'>通知失败</span>".format(item.paymentRemark);
                    if (item.timeDifference > 6 && item.noticeTimes == 5) {
                        result += ' <a class="btn btn-xs btn-warning" data-confirm="{0} <br>确定要<span class=orange>重置</span>该付款单？" data-href="/Payment/PaymentRecordReset?orderNo={0}">重置</a>'.format(item.orderNo);
                    }
                } else if (val == 4) {
                    result = '<span class="bold">通知中</span>';
                }
                return result;
            }
        },
        {
            field: 'noticeLastDate',
            title: replace_field('通知时间'),
            width: 120,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.FormatDate(val);
            }
        },
        { field: 'noticeTimes', title: replace_field('通知次数'), width: 65, align: 'left' },
        {
            field: 'paymentTypeName', title: replace_field('付款类型'), width: 65, align: 'left'
        }
    ]];

    $("#paymentRecord").datagrid({
        fit: false,
        loadMsg: '数据加载中，请稍后……',
        border: true,
        url: "/Payment/GetPaymentRecords",
        queryParams: getParams(),
        pagination: true,
        pageSize: pageSize,
        pageList: [15, 30, 50, 100, 1000],
        height: $(window).height() - 70,
        singleSelect: true,
        //fitColumns: true,
        rownumbers: true,
        showFooter: true,
        idField: 'orderNo',
        onLoadSuccess: function (data) {
            pageSize = $('#paymentRecord').datagrid('options').pageSize;
        },
        frozenColumns: frozenColumns,
        columns: columns
    });

}

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};